package com.jczy.cyclone.mmkv

import com.jczy.lib_utils.mmkv.MMKVOwner
import com.jczy.lib_utils.mmkv.MMKVProperty
import com.tencent.mmkv.MMKV


/**
 *@author Wcj
 *@description
 *@date 2022/6/27 14:56
 */
object AppMMKV : MMKVOwner {

    override val mmkv: MMKV
        get() = MMKV.mmkvWithID(MMKVConfig.ID.APP)

    //用户隐私协议
    var splashAgreement by MMKVProperty(false)
    //var loginAgreement = false
    // splashAgreement

    var isVip8 by MMKVProperty(false)

    //app引导页
    var needGuide by MMKVProperty(true)

    //爱车首页
    var needVehicleGuide by MMKVProperty(true)

    //上传app的版本
    var lastVersionCode by MMKVProperty(0)

    //记录启动时间
    var lastLaunchTime by MMKVProperty(0L)

    //车辆体检时间
    var lastExaminationTime by MMKVProperty(0L)

    //搜索记录
    var searchHistory by MMKVProperty("")

    //活动搜索历史记录
    var activitySearchHistory by MMKVProperty("")

    //路线搜索记录
    var routeSearchHistory by MMKVProperty("")

    // 俱乐部管理员相关搜索记录
    var clubMgrSearchHistory by MMKVProperty("")

    // 俱乐部管理员相关搜索记录
    var clubSearchHistory by MMKVProperty("")

    // 【商城】搜索记录
    var mallSearchHistory by MMKVProperty("")

    // 用户订单搜索记录
    var memberOrderSearchHistory by MMKVProperty("")

//    val MEID: String?
//        get() = UMConfigure.getUMIDString(appContext)

    //是否需要请求权限
    var requestPermission by MMKVProperty(false)
    var requestPhonePermission by MMKVProperty(false)

    //智能推送
    var smartPush by MMKVProperty(true)

    //未登录背景视频链接缓存
    var bgVideoUrl by MMKVProperty("")

    // 是否需要主动展示Rating弹窗
//    fun needShowRating(clubId: String): Boolean {
//        val key = "RATING_FLAG_" + clubId + "_" + UserMMKV.requiredUserId()
//        return !mmkv.getBoolean(key, false)
//    }
//
//    fun ignoreRating(clubId: String) {
//        val key = "RATING_FLAG_" + clubId + "_" + UserMMKV.requiredUserId()
//        mmkv.putBoolean(key, true)
//    }

    //获取缓存的体检id
    fun getExamId(vehicleId: String): String? {
        return mmkv.getString(vehicleId, null)
    }

    //保存体检的id
    fun saveExamId(vehicleId: String, id: String) {
        mmkv.putString(vehicleId, id)
    }

    // 上次加载的时间，确保每天只加载一次
    var lastShowAdDate by MMKVProperty("")

    // 判断今天是否需要展示商城首页广告
//    fun needShowHomeAd(): Boolean {
//        return lastShowAdDate != today()
//    }
//
//    fun cacheLastShowHomeAdDate() {
//        lastShowAdDate = today()
//    }

//    private fun today(): String =
//        DateUtil.formatDate(System.currentTimeMillis(), DateUtil.DATE_FORMAT_Y_M_D)

    // 上次刷新token时间
    var lastRefreshTokenDate by MMKVProperty(0L)

    // 判断是否刷新token
    fun needRefreshToken(): Boolean {
        return lastRefreshTokenDate != 0L && System.currentTimeMillis() - lastRefreshTokenDate > 10 * 86_400_000 //10天
    }

    fun cacheRefreshTokenDate() {
        lastRefreshTokenDate = System.currentTimeMillis()
    }


    /**用户位置缓存*/
    //上次位置信息
    var city by MMKVProperty("")
    var latitude by MMKVProperty(0.0)
    var longitude =0.0// by MMKVProperty(0.0)

    fun hasCachedLocation(): Boolean {
        return latitude != 0.0 && longitude != 0.0 && city.isNotEmpty()
    }
}